package com.dahaiwuliang.code.dao;

import com.dahaiwuliang.code.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleDao extends JpaRepository<Article,Integer> ,JpaSpecificationExecutor<Article> {

/**
 * 点击+1
 */
@Query(value = "update  article set click=click+1 where article_id=?1 ",nativeQuery = true)
@Modifying
public void updateClick(Integer articleId);

    /**
     * 今天发布
     */
    @Query(value = "select count(*) from article where  TO_DAYS(check_date)=TO_DAYS(NOW())",nativeQuery = true)
    Integer todayPublishNum();

    /**
     * 审核未通过的数
     */
    @Query(value = "select count(*) from  article where state=1 ",nativeQuery = true)
    Integer noAuditNum();

    @Query(value = "select count(*) from  article  ",nativeQuery = true)
    Integer articleCountNum();

    @Query(value = "select * from article where  state=2 order by  publish_date desc limit ?1",nativeQuery = true)
    List<Article> getNewArticle(Integer n);

    @Query(value = "select * from article where  state=2 order by  click desc limit ?1",nativeQuery = true)
    List<Article> getClickArticle(Integer n);


    @Query(value = "select * from article where  state=2 order by  RAND() desc limit ?1",nativeQuery = true)
    List<Article> getRandomArticle(Integer n);
}
